package com.esun.payment_api.service.impl;

import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.domain.SysDictData;
import com.esun.payment_api.domain.SysDictDataExample;
import com.esun.payment_api.mapper.SysDictDataMapper;
import com.esun.payment_api.security.AjaxResult;
import com.esun.payment_api.service.SysDictDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("SysDictDataService")
public class SysDictDataServiceImpl implements SysDictDataService {

    @Resource
    SysDictDataMapper sysDictDataMapper;

    @Override
    public List<SysDictData> selectByExample(Short coId, String dictType, String keyword, String word) {
        SysDictDataExample example = new SysDictDataExample();
        SysDictDataExample.Criteria criteria = example.createCriteria();
        //where条件
        criteria.andIsDeletedEqualTo("0");
        criteria.andStatusEqualTo("1");
        if (!BlankUtils.isBlank(coId)) {
            criteria.andCoIdEqualTo(coId);
        }
        if (!BlankUtils.isBlank(dictType)) {
            criteria.andDictTypeEqualTo(dictType);
        }
        if (!BlankUtils.isBlank(keyword) && !BlankUtils.isBlank(word)) {
            switch (keyword) {
                case "dictType":
                    criteria.andDictTypeLike("%" + word + "%");
                    break;
                case "dictKey":
                    criteria.andDictKeyLike("%" + word + "%");
                    break;
                case "dictValue":
                    criteria.andDictValueLike("%" + word + "%");
                    break;
                default:
                    System.out.println("输入参数有误！");
                    break;
            }
        }
        example.setOrderByClause("ORDER_NUM");
        return sysDictDataMapper.selectByExample(example);
    }

    @Override
    public AjaxResult insertSelective(SysDictData record) {
        try {
            if (sysDictDataMapper.insertSelective(record) == 1) {
                return AjaxResult.success("添加单条成功");
            } else {
                return AjaxResult.error("添加单条失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult deleteById(Short id) {
        try {
            if (sysDictDataMapper.deleteById(id) == 1) {
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult updateByPrimaryKeySelective(SysDictData record) {
        try {
            if (sysDictDataMapper.updateByPrimaryKeySelective(record) == 1) {
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public List<SysDictData> getDkBusinessType(Short userId) {
        return sysDictDataMapper.getDkBusinessType(userId);
    }
}
